<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/24
 * Time: 10:51
 */

namespace app\admin\controller;

use app\reuse\controller\ResponseJson;
use app\service\BaseService;
use app\service\CostService;
use think\Db;
use think\facade\Session;
class Cost extends Common
{
    use ResponseJson;

    function __construct()
    {
        parent::__construct();
    }
    /*
     * @param null
     * @content 费用列表
     * @return array
     * */
    function index()
    {
        if(input("get.active") == "ajax"){
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'),true);
            $_where = "";
            if (!empty($searchParams)) {
                # 商品名称 货号搜索
                if (!empty($searchParams['search'])) {
                    $_where .= "lc.title like '%" . $searchParams['search'] . "%'
                   ";
                }

            }

            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('log_cost')->alias('lc')
                ->leftJoin(['cost_category'=>'cc'],'lc.cate_id=cc.id')->field('lc.*,cc.title cate_title')->order("create_time desc")->where($_where)->paginate($listRows, false, ['query' => request()->param()]);
            $data=$info->toArray();
            foreach($data['data'] as &$v)
            {
                $v['cate_title']=empty($v['cate_title'])?$v['title']:$v['cate_title'];
            }
            //dump($info->toArray());exit;
            return $this->jsonSuccessData($data);
        }else{
            return $this->fetch();

        }

    }
    /*
     * @param null
     * @content 费用创建 编辑
     * @return array
     * */
    function saveinfo(){
        if(input("get.active") == 'ajax'){
            // 是否ajax
            if (!IS_AJAX) {
                return $this->error('非法访问！');
            }
            // 开始操作 接收参数
            $params = $this->data_post;
            $data = $params;

            CostService::SaveCate($data,$params);
            return DataReturn('保存成功', 0);

        }else{
           /* $id = input("get.id");
            if(!empty($id)){
                #编辑
                $info = Db::table('log_cost')
                    ->where("id = $id")
                    ->find();
                $this->assign('info',$info);
                $this->assign('id',$id);
            }*/
            $data_params  = [
                'where' => [['id','>',0]],
                'page' => true,
                'field' => '*',
                'number' => 10,
                'table'=>'cost_category'
            ];
            $costcates= BaseService::DataList($data_params);
            $this->assign('costcates',$costcates);
            return $this->fetch();
        }
    }




    /**
     * [costcate 分类列表].
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月21日11:40:22
     */
    public function costcate()
    {

        if ('ajax' != input('get.action')) {
            return $this->fetch();
        } else {
            $data_params  = [
                'where' => [['id','>',0]],
                'page' => true,
                'field' => '*',
                'number' => 10,
                'table'=>'cost_category'
            ];
            $res= BaseService::DataList($data_params);

            $total = count($res);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $res];
        }



    }

    /**
     * [Save 分类添加/编辑].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function Save()
    {
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }
        // 开始操作
        $params = $this->data_post;

        //整理数据
        BaseService::SaveInfo($params,['table'=>'cost_category']);

        return DataReturn('保存成功', 0);
    }



    /**
     * [删除分类].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日17:02:36
     */
    public function Delcate()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }
        $params=$this->data_post;
        $params['table']='cost_category';
        BaseService::DelInfo($params);

        return DataReturn('删除成功', 0);
    }

}